<template>
  <div class="admin-container">
    <div class="sidebar-container">
    <div class="sidebar-header">
        <h1 class="ceb-title">CEB</h1>
      </div>
    <!-- 侧边栏导航 -->
    <el-menu
      class="sidebar-menu"
      :default-active="activeMenu"
      @select="handleMenuSelect"
      unique-opened
    >
      <!-- 仪表盘 -->
      <el-menu-item index="dashboard">
        <el-icon><PieChart /></el-icon>
        <span>仪表盘</span>
      </el-menu-item>

      <!-- 用户管理 -->
      <el-sub-menu index="user">
        <template #title>
          <el-icon><User /></el-icon>
          <span>用户管理</span>
        </template>
        <el-menu-item index="userList">用户列表</el-menu-item>
        <el-menu-item index="approvalUser">待审核用户</el-menu-item>
      </el-sub-menu>

      <!-- 商品管理 -->
      <el-sub-menu index="product">
        <template #title>
          <el-icon><ShoppingCart /></el-icon>
          <span>商品管理</span>
        </template>
        <el-menu-item index="productList">商品列表</el-menu-item>
        <el-menu-item index="approvalProduct">待审核商品</el-menu-item>
      </el-sub-menu>

      <!-- 订单管理 -->
      <el-sub-menu index="order">
        <template #title>
          <el-icon><Tickets /></el-icon>
          <span>订单管理</span>
        </template>
        <el-menu-item index="orderList">所有订单</el-menu-item>
        <el-menu-item index="refund">退款处理</el-menu-item>
      </el-sub-menu>

      <!-- 论坛管理 -->
      <el-sub-menu index="forum">
        <template #title>
          <el-icon><ChatDotRound /></el-icon>
          <span>论坛管理</span>
        </template>
        <el-menu-item index="topicList">帖子管理</el-menu-item>
        <el-menu-item index="commentList">评论审核</el-menu-item>
      </el-sub-menu>
       <!-- 公告管理 -->
       <el-sub-menu index="notice">
        <template #title>
          <el-icon><Document /></el-icon>
          <span>公告管理</span>
        </template>
        <el-menu-item index="noticeList">公告列表</el-menu-item>
        <!-- <el-menu-item index="addNotice">发布公告</el-menu-item> -->
      </el-sub-menu>
    </el-menu>
  </div>
    <!-- 主内容区 -->
    <div class="main-content">
      <!-- 顶部导航 -->
      <div class="header">
        <h1>CampusEasyBuy 管理后台</h1>
        <div class="user-info">
          <el-button type="danger" @click="handleLogout">退出登录</el-button>
        </div>
      </div>

      <!-- 内容区 -->
      <div class="content-wrapper">
        <transition name="fade" mode="out-in">
          <component :is="currentComponent" />
        </transition>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, shallowRef } from 'vue';
import { useRouter } from 'vue-router';
import { ElMessageBox } from 'element-plus';
import {
  PieChart,
  User,
  ShoppingCart,
  Tickets,
  ChatDotRound,
  Document
} from '@element-plus/icons-vue';

// 导入组件（根据实际路径调整）
import Dashboard from './components/Dashboard.vue';
import UserList from './components/UserList.vue';
import ApprovalUser from './components/ApprovalUser.vue';
import ProductList from './components/ProductList.vue';
import ApprovalProduct from './components/ApprovalProduct.vue';
// import Category from './components/Category.vue';
import OrderList from './components/OrderList.vue';
// import Refund from './components/Refund.vue';
import TopicList from './components/TopicList.vue';
import CommentList from './components/CommentList.vue';
import NoticeList from './components/NoticeList.vue';

const router = useRouter();
const activeMenu = ref('dashboard');
const currentComponent = shallowRef(Dashboard);

// 处理菜单选择
const handleMenuSelect = (index) => {
  activeMenu.value = index;
  switch(index) {
    case 'dashboard':
      currentComponent.value = Dashboard;
      break;
    case 'userList':
      currentComponent.value = UserList;
      break;
    case 'approvalUser':
      currentComponent.value = ApprovalUser;
      break;
    case 'productList':
      currentComponent.value = ProductList;
      break;
    case 'approvalProduct':
      currentComponent.value = ApprovalProduct;
      break;
    // case 'category':
    //   currentComponent.value = Category;
    //   break;
    case 'orderList':
      currentComponent.value = OrderList;
      break;
    // case 'refund':
    //   currentComponent.value = Refund;
    //   break;
    case 'topicList':
      currentComponent.value = TopicList;
      break;
    case 'noticeList':
      currentComponent.value = NoticeList;
      break;
    case 'commentList':
      currentComponent.value = CommentList;
      break;
    // default:
    //   currentComponent.value = Dashboard;
  }
};

// 处理退出登录
const handleLogout = () => {
  ElMessageBox.confirm('确定要退出登录吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    localStorage.removeItem('adminToken');
    router.push('/adminlogin');
  });
};
</script>

<style scoped>
/* 终极无空白解决方案 */
.admin-container {
  display: flex;
  height: 100vh;
  margin: -1px 0 0 -1px; /* 抵消潜在边框偏移 */
  padding: 0;
  background: #f5f6f8;
  overflow: hidden;
}
.sidebar-container {
  width: 240px;
  height: 100vh;
  background: #fff;
  border-right: 1px solid #e6e6e6;
}

.sidebar-header {
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-bottom: 1px solid #e6e6e6;
}

.ceb-title {
  margin: 0;
  font-size: 28px;
  font-weight: 700;
  color: #409eff;
  letter-spacing: 2px;
}

.sidebar-menu {
  height: calc(100vh - 60px) !important;
  border-right: none !important;
}

.main-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  margin: 0;
  padding: 0;
}
/* 新增右侧容器样式 */
.user-info {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-left: auto; /* 强制靠右 */
}
.header {
  flex: 0 0 60px;
  display: flex;
  justify-content: space-between; /* 新增这个属性 */
  align-items: center;
  padding: 0 20px !important;
  background: #fff;
  box-shadow: 0 0 8px rgba(0, 0, 0, 0.1);
  position: relative;
  z-index: 1;
}

.header h1 {
  margin: 0;
  padding: 0;
  font-size: 20px;
  line-height: 1;
  position: relative;
  top: -1px; /* 微调字体对齐 */
}

.content-wrapper {
  flex: 1;
  padding: 0; /* 完全移除内边距 */
  overflow-y: auto;
}

.user-info {
  display: flex;
  align-items: center;
  margin: 0;
  padding: 0;
}

/* 强制菜单项绝对紧凑 */
.el-menu-item {
  min-height: 46px !important;
  height: 46px !important;
  line-height: 46px !important;
  margin: 0 !important;
  padding: 0 16px !important;
}

.el-sub-menu__title {
  height: 46px !important;
  line-height: 46px !important;
  margin: 0 !important;
  padding: 0 16px !important;
}
</style>